package com.LeeCode;

/**
 * 最长优雅子数组
 */

public class Code2401 {
    public static void main(String[] args) {
        int[] nums = {1, 3, 8, 48, 10};
        System.out.println(new Code2401().longestNiceSubarray(nums));
        Integer a  = Integer.valueOf(1);
    }

    public int longestNiceSubarray(int[] nums) {
        int n = nums.length;
        int left = 0;
        int bitMask = 0;
        int maxLen = 0;

        for (int right = 0; right < n; right++) {
            // 如果当前元素与已有元素有位重叠，缩小窗口
            while ((bitMask & nums[right]) != 0) {
                // 移除最左边的元素对 bitMask 的影响
                bitMask ^= nums[left];
                left++;
            }

            // 更新 bitMask
            bitMask |= nums[right];

            // 更新最长子数组长度
            maxLen = Math.max(maxLen, right - left + 1);
        }

        return maxLen;
    }
}
